//公共头部
Vue.component('public-header',{
	template: '<li class="nav-item dropdown">\
						<a class="nav-link nav-link" href="" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">\
						<img :src="_headpic" class="img-avatar">\
						</a>\
						<div class="dropdown-menu dropdown-menu-right">\
							<div class="dropdown-header text-center">\
								<strong>设置</strong>\
							</div>\
							<a class="dropdown-item" href="#"><i class="fa fa-user"></i>欢迎{{user}}</a>\
							<a class="dropdown-item" @click="updatePwd"><i class="icon-lock-open"></i>修改密码</a>\
							<a class="dropdown-item" @click="logout"><i class="fa fa-lock"></i> 退出</a>\
						</div>\
					</li>',
	data:function(){
		return {
			headpic:'',
			user:'',
			userId:''
		}
	},
	props: {
		type: {
			type: String,
			default: ''
		}
	},
	computed:{
		_headpic:function(){
			return this.headpic==''?'/static/simpliq/img/avatars/6.jpg':this.headpic;
		}
	},
	methods:{
		getUserinfo:function(){
			var _this=this;
			_this.$http('get',_this.type+'/'+_this.type+'/'+this.userId,'',function(res){
				if(_this.type=='admin'){
					_this.headpic=res.data.adm_headpic;
					_this.user=res.data.adm_user;
				}else if(_this.type=='user'){
					_this.headpic=res.data.user_headpic;
					_this.user=res.data.user_name;
				}
			})
		},
		logout:function(){
			var _this=this;
			this.$http('get',this.type+'/index/logout','',function(){
				_this.$vt_message('success','退出成功',1,function(){
					location.href='/'+_this.type+'/index/login';
				})
			})
		},
        updatePwd:function(){
            location.href='/'+this.type+'/index/updatepwd';
        }
	}
	,
	created:function(){
		//根据不同端调用处理
		if(this.type=='admin'){
			this.userId=JSON.parse(localStorage.getItem('user_info')).adm_id;
		}else if(this.type=='user'){
			this.userId=JSON.parse(localStorage.getItem('user_info')).user_id;
		}
		this.getUserinfo();
	}
});
//菜单
Vue.component('public-sidebar',{
	template: '<div class="sidebar"  >\
				<nav class="sidebar-nav" v-show="menus" >\
					<ul class="nav">\
					<template v-for="item in menus">\
					  <template v-if="item.children">\
						<li class="nav-item nav-dropdown" >\
							<a class="nav-link nav-dropdown-toggle" href="#" :name="item.point"><i :class="item.icon"></i>{{item.name}}</a>\
							<ul class="nav-dropdown-items">\
								<template v-for="child in item.children">\
									<li class="nav-item" >\
										<a class="nav-link" :href="child.url" :name="child.point"><i :class="child.icon"></i>{{child.name}}</a>\
									</li>\
								</template>\
							</ul>\
						</li>\
					  </template>\
					  <template v-else>\
					  	<li class="nav-item" >\
							<a class="nav-link" :href="item.url" :name="item.point"><i :class="item.icon" ></i>{{item.name}}</a>\
						</li>\
					  </template>\
					</template>\
					</ul>\
					</nav>\
					<button class="sidebar-minimizer brand-minimizer" type="button" v-show="menus"></button>\
				</div>',
	data:function(){
		return {
			menus:''
		}
	},
	methods:{
		getMenus:function(){
			var _this=this;
			this.$http('post','/common/menu/index',{menu:JSON.parse(localStorage.getItem('user_info')).menus},function(res){
				if(res){
					_this.menus=res.data;
                    if(_this.menus){
                        setTimeout(function(){
                            _this.currentLink();
                        },30);
                    }
				}
			})
		},
        currentLink:function(){
            //添加选中样式 需改写 使用ref
            $.navigation = $('nav > ul.nav');
            $.navigation.find('a').each(function(){
                var cUrl = String(window.location).split('?')[0];
                if (cUrl.substr(cUrl.length - 1) == '#') {
                    cUrl = cUrl.slice(0,-1);
                }
                //暂时去掉html
                if(cUrl.indexOf('.html')>0){
                    cUrl = cUrl.replace('.html', '');
                }
                if ($($(this))[0].href==cUrl) {
                    $(this).addClass('active');
                    $(this).parents('ul').add(this).each(function(){
                        $(this).parent().addClass('open');
                    });
                }else{
                    var index= String(window.location).indexOf('#');
                    if(index!=-1){
                        //没有的话 定义到父级
                        var menu=String(window.location).substring(index+1);
                        if($($(this))[0].name==menu){
                            $(this).addClass('active');
                            $(this).parent().toggleClass('open');
                        }
                    }
                }
            });
        }
	}
	,
	created:function(){
        this.$Spin.show();
		this.getMenus();
	},
    beforeUpdate:function(){
        this.$Spin.hide();
    }
})